﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;
using System.Web.SessionState;
namespace jnxc.Web.teaminfo
{
    /// <summary>
    /// GetTeam 的摘要说明
    /// </summary>
    public class GetTeam : IHttpHandler, IRequiresSessionState
    {
        jnxc.BLL.teaminfo bll = new jnxc.BLL.teaminfo();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "querymy")
                Querymy(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["mid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int tid = 0;
                string htpic = context.Request.Form["htpic"];
                string uidstr = context.Request.Form["tid"];
                if (uidstr != "")
                    tid = int.Parse(uidstr);
                string savepath = "";
                HttpFileCollection files = context.Request.Files;
                if (files.Count == 1)
                {
                    HttpPostedFile file = files[0];
                    if (file.ContentLength > 0)
                    {
                        //全路径  
                        string FullFullName = file.FileName;
                        //获取图片的名称  
                        String fileName = FullFullName.Substring(FullFullName.LastIndexOf("\\") + 1);
                        string exp = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
                        string filenames = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        string path = "~/teampics";
                        savepath = "/teampics";
                        if (exp == ".jpg" || exp == ".bmp" || exp == ".gif" || exp == ".png")
                        {
                            file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + filenames + exp);
                            savepath += "/" + filenames + exp;
                        }
                        else
                        {
                            context.Response.Write("P");//返回给前台页面  
                            context.Response.End();

                        }
                    }
                    else
                    {
                        savepath = htpic;

                    }
                }
                else
                {
                    savepath=htpic;
                }                   
                string tbh = context.Request.Form["tbh"];
                string tname = context.Request.Form["tname"];
                string tsex = "男";
                if (context.Request.Form["rsexwoman"] != null)
                    tsex = "女";
                string tno = context.Request.Form["tno"];
                string tphone = context.Request.Form["tphone"];
                string temail = context.Request.Form["temail"];
                string tpost = context.Request.Form["tpost"];
                string tarea = context.Request.Form["tarea"];
                string tjob = context.Request.Form["tjob"];
                string tedu = context.Request.Form["tedu"];
                string strtime = context.Request.Form["ttime"];
                DateTime? ttime = null;
                if (strtime != "")
                    ttime = DateTime.Parse(context.Request.Form["ttime"]);
                string thonour = context.Request.Form["thonour"];
                jnxc.Model.teaminfo model = new jnxc.Model.teaminfo();
                model.tbh = tbh;
                model.tname = tname;
                model.tsex = tsex;
                model.tno = tno;
                model.tphone = tphone;
                model.temail = temail;
                model.tpost = tpost;
                model.tarea = tarea;
                model.tjob = tjob;
                model.tedu = tedu;
                model.ttime = ttime;
                model.thonour = thonour;
                model.tpic = savepath;
                model.uid = uid;
                model.tid = tid;
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string savepath = "";
                HttpFileCollection files = context.Request.Files;
                if (files.Count == 1)
                {
                    HttpPostedFile file = files[0];
                    if (file.ContentLength > 0)
                    {
                        //全路径  
                        string FullFullName = file.FileName;
                        //获取图片的名称  
                        String fileName = FullFullName.Substring(FullFullName.LastIndexOf("\\") + 1);
                         string exp = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
                        string filenames = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        string path = "~/teampics";
                        savepath = "/teampics";
                        if (exp == ".jpg" || exp == ".bmp" || exp == ".gif" || exp == ".png")
                        {
                            file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path) + "\\" + filenames + exp);
                            savepath += "/" + filenames + exp;
                        }
                        else
                        {
                            context.Response.Write("P");//返回给前台页面  
                            context.Response.End();

                        }
                    }
                }
                string tbh = context.Request.Form["tbh"];
                string tname = context.Request.Form["tname"];
                string tsex = "男"; 
                if(context.Request.Form["rsexwoman"]!=null)
                    tsex="女";
                string tno = context.Request.Form["tno"];
                string tphone = context.Request.Form["tphone"];
                string temail = context.Request.Form["temail"];
                string tpost = context.Request.Form["tpost"];
                string tarea = context.Request.Form["tarea"];
                string tjob = context.Request.Form["tjob"];
                string tedu = context.Request.Form["tedu"];
                string strtime = context.Request.Form["ttime"];
                DateTime? ttime=null;
                if(strtime!="")
                 ttime= DateTime.Parse(context.Request.Form["ttime"]);
                string thonour = context.Request.Form["thonour"]; 
                jnxc.Model.teaminfo model = new jnxc.Model.teaminfo();
                model.tbh = tbh;
                model.tname = tname;
                model.tsex = tsex;
                model.tno = tno;
                model.tphone = tphone;
                model.temail = temail;
                model.tpost = tpost;
                model.tarea = tarea;
                model.tjob = tjob;
                model.tedu = tedu;
                model.ttime = ttime;
                model.thonour = thonour;
                model.tpic = savepath;
                model.uid = uid;
                if (bll.Add(model) > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void Querymy(HttpContext context)
        {


            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int level = Web.Admin.myinfo.getLevel(context);
                string  uprovince, ucity, ucounty;
                uprovince = ucity = ucounty = "";
                string uname, utruename,unit;
                uname = utruename = unit = "";
                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                if (null != context.Request.Form["uname"])
                {//获取前台传来的值  
                    uname = context.Request.Form["uname"].ToString().Trim();
                }
                if (null != context.Request.Form["utruename"])
                {//获取前台传来的值  
                    utruename = context.Request.Form["utruename"].ToString().Trim();
                }
                if (null != context.Request.Form["unit"])
                {//获取前台传来的值  
                    unit = context.Request.Form["unit"].ToString().Trim();
                }
                if (null != context.Request.Form["uprovince"])
                {
                    uprovince = context.Request.Form["uprovince"].ToString().Trim();
                }
                if (null != context.Request.Form["ucity"])
                {
                    ucity = context.Request.Form["ucity"].ToString().Trim();
                }
                if (null != context.Request.Form["ucounty"])
                {
                    ucounty = context.Request.Form["ucounty"].ToString().Trim();
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" tname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" tjob like '%{0}%' and ", utruename);
                }
                if (unit != "")
                {
                    strWhere.AppendFormat(" tedu like '%{0}%' and ", unit);
                }
                if (level <= 3)
                    uprovince = pcc[0];
                if (level <= 2)
                {
                    ucity = pcc[1];
                }
                if (level <= 1)
                {
                    ucounty = pcc[2];
                }                
                    if (ucounty != "")
                    {
                        strWhere.AppendFormat(" ucounty='{0}' and ", ucounty);
                    }            
                
                    if (ucity != "")
                    {
                        strWhere.AppendFormat(" ucity='{0}' and ", ucity);
                    }               
                
                    if (uprovince != "")
                    {
                        strWhere.AppendFormat(" uprovince='{0}' and ", uprovince);

                    }
                
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void Query(HttpContext context)
        {


            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {

                string uname, utruename, st;
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" tname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" tbh like '%{0}%' and ", utruename);
                }
                strWhere.AppendFormat(" uid={0} and ", uid);
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}